﻿using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace DotNetRepository.LinqToSql
{
    internal class CustomDataContext : DataContext
    {
        internal CustomDataContext(string connectionString, MappingSource source) : base(connectionString, source)
        {}

        
        internal List<TEntity> ExecuteMethodCall<TEntity>(params object[] parameters) where TEntity : class, IEntity
        {
            //var sourceAttribute = (FunctionAttribute)new StackTrace().GetFrame(2).GetMethod().GetCustomAttributes(typeof(FunctionAttribute), false).Single();
            var methodInfo = (MethodInfo) new StackTrace().GetFrame(2).GetMethod();
            //var methodInfo = (MethodInfo) MethodBase.GetCurrentMethod();
            //var targetAttribute = (FunctionAttribute)methodInfo.GetCustomAttributes(typeof (FunctionAttribute), false).Single();
            //targetAttribute.Name = sourceAttribute.Name;

            var result = ExecuteMethodCall(this, methodInfo, parameters);
            var typedResult = (ISingleResult<TEntity>)result.ReturnValue;
            return typedResult.ToList();
        }

    }
}
